<template>
	<base-page bgColor="#FFFFFF">
		<view class="login-box" slot="center">
			<view class="logo"><image :src="staticImgUrl + 'logoWxlogin.png'" mode=""></image></view>
			<view class="logo-name">醒目购</view>
			<view class="logo-text">醒目购，够醒目，醒目就购了！</view>
			<view class="btn-box">
				<button class="login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键登录</button>
				<view class="login-btn-two" @click="BackPage()">暂不登录</view>
			</view>
		</view>
	</base-page>
</template>
<script>
import $Axios from '@/common/js/request/http.js';
import { mapGetters, mapMutations } from 'vuex';
export default {
	data() {
		return {};
	},
	computed: {
		...mapGetters(['userInfo'])
	},
	components: {},
	async onLoad(e) {},
	mounted() {
		this.pagesLen = getCurrentPages().length;
	},
	methods: {
		...mapMutations(['SET_USER_TOKEN', 'SET_USER_INFO']),
		BackPage() {
			// 判断页面栈是否只有一个
			if (this.pagesLen == 1) {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			} else {
				uni.navigateBack({
					delta: 1
				});
			}
		},
		getPhoneNumber(e) {
			console.log('点击了')
			if (uni.getStorageSync('code')) {
				if (e.target.errMsg == 'getPhoneNumber:ok') {
					let newUserInfo = this.$Utils.deepCopy(this.userInfo);
					let options = {
						iv: e.detail.iv,
						encryptedData: e.detail.encryptedData
					};
					
					// console.log("uni.getStorageSync('code')",uni.getStorageSync('code'))
					// console.log("uni.getStorageSync('iv')",uni.getStorageSync('iv'))
					// console.log("uni.getStorageSync('encryptedData')",uni.getStorageSync('encryptedData'))
					// console.log("options.iv",options.iv)
					// console.log("options.encryptedData",options.encryptedData)
					
					this.$Apis
						.weappLogin1({
							code: uni.getStorageSync('code'),
							user_iv: uni.getStorageSync('iv'),
							user_encryptedData: uni.getStorageSync('encryptedData'),
							mobile_iv: options.iv,
							mobile_encryptedData: options.encryptedData
						})
						.then(res => {
							console.log("res=>",res)
							this.SET_USER_TOKEN(res.token);
							uni.setStorageSync('token', res.token);
							// 判断是否填写过资料
							if (!res.is_first) {
								let newObj = {
									token: res.token,
									headimg: newUserInfo.headimg,
									nickname: newUserInfo.nickname,
									sex: newUserInfo.sex
								};
								this.$Apis.modifyInfo(newObj).then(succ => {
									this.$Apis
										.getUserInfo({
											token: res.token
										})
										.then(info => {
											this.SET_USER_INFO(info);
											this.$Router.reLaunch({
												name: 'Home'
											});
										});
								}).catch(errResult=>{
									console.log("errResult",errResult)
								})
							} else {
								this.$Router.reLaunch({
									name: 'Info'
								});
							}
						}).catch(err=>{
							console.log("err",err)
						})
				}
			} else {
				this.$Router.reLaunch({
					name: 'Login'
				});
			}
		}
	},
	onLoad() {}
};
</script>

<style lang="scss" scoped>
image {
	width: 100%;
	height: 100%;
}

.login-box {
	width: 100%;
	padding-top: 348rpx;

	.logo {
		width: 184rpx;
		height: 184rpx;
		margin: 0 auto;
	}

	.logo-name {
		font-size: 48rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #222222;
		text-align: center;
		padding-top: 43rpx;
		padding-bottom: 30rpx;
	}

	.logo-text {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #999999;
		text-align: center;
	}
}

.btn-box {
	position: absolute;
	top: 80%;
	left: 50%;
	transform: translate(-50%, -50%);

	.login-btn {
		width: 560rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #ff7044, #fd3b5c);
		border-radius: 44rpx;
		margin: 0 auto;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #ffffff;
		line-height: 88rpx;
		text-align: center;
	}

	.login-btn-two {
		width: 560rpx;
		height: 88rpx;
		border: 1px solid #999999;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #999999;
		line-height: 88rpx;
		text-align: center;
		margin: 50rpx auto 0 auto;
	}
}
</style>
